define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiservice/orderz/index' + location.search,
                    add_url: 'xiservice/orderz/add',
                    edit_url: 'xiservice/orderz/edit',
                    del_url: 'xiservice/orderz/del',
                    multi_url: 'xiservice/orderz/multi',
                    import_url: 'xiservice/orderz/import',
                    table: 'xiservice_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE',visible:false}, 
                        {field: 'service_id', title: __('订单信息'), operate:false, formatter: function (val,row) {
                              var html='';
                               html += '<div style="text-align:left">'+row.order_no+'</div>';
                           
                               html += '<div style="text-align:left">服务时长:无时长</div>';
                             return html;
                        }},                             
                        {field: 'user.username', title: __('用户信息'), operate:false, formatter: function (val,row) {
                              var html='';
                              html += '<div style="text-align:left">'+row.user.username+'</div>';
                              html += '<div style="text-align:left">'+row.user.mobile+'</div>';
                              if(row.addr){
                                html += '<div style="text-align:left">下单地址:'+row.addr+'</div>';
                            }else{
                               html += '<div style="text-align:left">下单地址:'+'-'+'</div>';
                            }
                             return html;
                        }},                                                
                        {field: 'user.username', title: __('用户姓名'), operate:'LIKE',visible:false}, 
                        {field: 'user.mobile', title: __('手机号码'), operate:'LIKE',visible:false},                   
                        {field: 'total_money', title: __('费用信息'), operate:false, formatter: function (val,row) {
                              var html='';
                               html += '<div style="text-align:left">总金额:'+row.total_money+'元</div>';
                               html += '<div style="text-align:left">实付金额:'+row.pay_money+'元</div>';
                             return html;
                        }},  
                        {field: 'transaction_id', title: __('支付信息'), operate:false, formatter: function (val,row) {
                              var html='';
                               html += '<div style="text-align:left">'+row.pay_type_text+' '+row.pay_status_text+'</div>';
                               html += '<div style="text-align:left">支付状态:'+row.pay_status_text+'</div>';
                               html += '<div style="text-align:left">支付时间:'+row.pay_time_text+'</div>';
                            //    if(row.transaction_id){
                            //      html += '<div style="text-align:left">回调信息:'+row.transaction_id+'</div>';
                            //    }
                             return html;
                        }},    
                        
                        {field: 'task_mode', title: __('订单模式'), searchList: {"0":__('固定价格'),"1":__('固定价格')}, formatter: Table.api.formatter.status},
                        {field: 'order_status', title: __('订单状态'), searchList: {"40":__('服务中'),"50":__('已完成')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                
                                {
                                    name: 'addtabs',
                                    text: __('订单详情'),
                                    title: __('订单详情'),
                                    classname: 'btn btn-xs btn-warning  btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'xiservice/order/detail?ids={id}',
                                    
                                }
                            ],
                            
                            formatter: Table.api.formatter.buttons,}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'xiservice/orderz/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'xiservice/order/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'xiservice/order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        }, 
        changeworker:function(){
           Controller.api.bindevent();
        },      
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
